﻿using System;
using System.IO;
using System.Net;
using MefBasic.Threading;

namespace MediaAssistant.DAL.Helper
{
    public class WebRequestHelper
    {
        public static bool Download(string url, string fileName)
        {
            return Download(url, new FileStream(fileName, FileMode.Create));
        }

        public static bool Download(string url, Stream stream)
        {
            try
            {
                if (url.Equals("N/A", StringComparison.OrdinalIgnoreCase))
                    return false;
                byte[] content;
                var request = (HttpWebRequest)WebRequest.Create(url);
                var response = request.GetResponse();

                var responseStream = response.GetResponseStream();
                if (responseStream == null)
                    return false;
                using (var br = new BinaryReader(responseStream))
                {
                    content = br.ReadBytes(500000);
                    br.Close();
                }
                response.Close();

                var bw = new BinaryWriter(stream);
                try
                {
                    bw.Write(content);
                }
                finally
                {
                    stream.Close();
                    bw.Close();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static string GetContent(string url)
        {
            if (url.Equals("N/A", StringComparison.OrdinalIgnoreCase))
                return string.Empty;
            var request = (HttpWebRequest)WebRequest.Create(url);
            var response = request.GetResponse();

            var stream = response.GetResponseStream();
            if (stream == null)
                return String.Empty;
            var streamReader = new StreamReader(stream);
            var content = streamReader.ReadToEnd();
            streamReader.Close();
            return content;
        }

        public static byte[] DownloadBytesInBlockedThread(string url)
        {
            var job = new Job(DownloadBytesInBackground);
            job.Store.Add(url,"URL");
            job.StartBlocked();
            if (job.Store.ContainsKey("Bytes"))
                return (byte[]) job.Store.GetObject("Bytes");
            return null;
        }

        private static void DownloadBytesInBackground(Job obj)
        {
            try
            {
                var url = (string)obj.Store.GetObject("URL");
                var buffer = DownloadInCurrentThread(url);
                obj.Store.Add(buffer, "Bytes");
            }
            catch (Exception)
            {
                
            }
        }

        public static byte[] DownloadInCurrentThread(string url)
        {
            var memoryStream = new MemoryStream();
            Download(url, memoryStream);
            var buffer = memoryStream.GetBuffer();
            memoryStream.Close();
            return buffer;
        }

        public static void UpdateImageInBackground(Movie movie)
        {
            var job = new Job(UpdateImageInBackground);
            job.Store.Add(movie, "Movie");
            job.Start();
        }

        private static void UpdateImageInBackground(Job obj)
        {
            try
            {
                var movie = (Movie)obj.Store.GetObject("Movie");
                DatabaseManager.Instance.UpdatePosterImage(movie, DownloadInCurrentThread(movie.Poster));
            }
            catch (Exception)
            {

            }
        }
    }
}
